//
//  HLIMKItRealVoiceContainer.m
//  YueYue
//
//  Created by hulu001 on 2024/1/12.
//

#import "HLIMKItRealVoiceContainer.h"
#import "HLIMKitVoiceDBView.h"

@interface HLIMKItRealVoiceContainer () <UITableViewDelegate, UITableViewDataSource>
@property (strong,  nonatomic) HLIMKitVoiceDBView *voiceDBView;
@property (strong,  nonatomic) UIActivityIndicatorView *activityIndicatorView;
@property (strong,  nonatomic) UILabel *activityLabel;
@property (strong,  nonatomic) UIButton *selectButton;
@property (strong,  nonatomic) UIButton *finishButton;
@property (strong,  nonatomic) UIButton *sendButton;

@property (strong,  nonatomic) UITableView *tableView;
@property (strong,  nonatomic) NSArray *languages;

@end

@implementation HLIMKItRealVoiceContainer


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:250/255.0 green:251/255.0 blue:252/255.0 alpha:1.0];
        
//        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//        button.frame = CGRectMake(50, 0, CGRectGetWidth(frame) - 100, 44);
//        button.titleLabel.font = [UIFont systemFontOfSize:15];
//        [button setTitle:@"中转越" forState:UIControlStateNormal];
//        [button setTitleColor:[UIColor colorWithRed:99/255.0 green:104/255.0 blue:118/255.0 alpha:1.0] forState:UIControlStateNormal];
//        [button setImage:[UIImage imageNamed:@"imBarJian"] forState:UIControlStateNormal];
//        [button addTarget:self action:@selector(changeLanguage:) forControlEvents:UIControlEventTouchUpInside];
//        [self addSubview:button];
//        self.selectButton = button;
        
        self.voiceDBView = [HLIMKitVoiceDBView.alloc initWithFrame:CGRectMake(55, 0, CGRectGetWidth(frame) - 110, 40)];
        CGPoint center = CGPointMake(CGRectGetWidth(frame) * 0.5, CGRectGetHeight(frame) * 0.5);
        self.voiceDBView.center = center;
        self.voiceDBView.lineColor = [UIColor colorWithRed:168/255.0 green:171/255.0 blue:185/255.0 alpha:1.0];
        [self addSubview:self.voiceDBView];
        
        UIButton *finishButton = [UIButton buttonWithType:UIButtonTypeCustom];
        finishButton.viewSize = CGSizeMake(60, 40);
        finishButton.centerX = self.middleX;
        finishButton.bottom = CGRectGetHeight(frame);
        finishButton.titleLabel.font = [UIFont systemFontOfSize:15];
        [finishButton setTitle:@"说完了" forState:normal];
        [finishButton setTitleColor:[UIColor colorWithRed:99/255.0 green:104/255.0 blue:118/255.0 alpha:1.0] forState:UIControlStateNormal];
        [finishButton addTarget:self action:@selector(finish) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:finishButton];
        self.finishButton = finishButton;
        
        self.sendButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.sendButton.viewSize = CGSizeMake(120, 40);
        self.sendButton.center = self.middlePoint;
        self.sendButton.titleLabel.font = [UIFont systemFontOfSize:16 weight:UIFontWeightMedium];
        [self.sendButton setTitle:@"点 击 发 送" forState:UIControlStateNormal];
        [self.sendButton setTitleColor:[UIColor colorWithRed:148/255.0 green:106/255.0 blue:250/255.0 alpha:1.0] forState:UIControlStateNormal];
        [self.sendButton addTarget:self action:@selector(sendClicked:) forControlEvents:UIControlEventTouchUpInside];
        self.sendButton.hidden = YES;
        [self addSubview:self.sendButton];
    }
    return self;
}

- (void)changeValue:(CGFloat)value
{
    [self.voiceDBView addValue:@(value)];
}

- (void)changeLanguage:(UIButton *)button
{
    if (!button.selected) {
        if (!_tableView) {
            self.languages = @[@"中转越",@"中转英",@"越转中",@"英转中"];
            if ([self.delegate respondsToSelector:@selector(imkit_languagesForRealVoice)]) {
                self.languages = [self.delegate imkit_languagesForRealVoice];
            }
            
            _tableView = [UITableView.alloc initWithFrame:CGRectMake(0, 40, self.width, self.height-40) style:UITableViewStylePlain];
            _tableView.backgroundColor = self.backgroundColor;
            _tableView.delegate = self;
            _tableView.dataSource = self;
            _tableView.rowHeight = 44;
            [self addSubview:_tableView];
        }
    } else {
        [_tableView removeFromSuperview];
        _tableView = nil;
    }
    
    button.selected = !button.selected;
}

- (void)setStatus:(HLIMRealVoiceStatus)status
{
    _status = status;
    
    switch (status) {
        case HLIMRealVoiceStatusRecording:
        {   // 录音中
            self.voiceDBView.hidden = NO;
            self.selectButton.hidden = NO;
            self.finishButton.hidden = NO;
            self.sendButton.hidden = YES;
            [self.voiceDBView reset];
        }
            break;
        case HLIMRealVoiceStatusTranslacting:
        {   // 翻译中
            self.voiceDBView.hidden = YES;
            self.selectButton.hidden = YES;
            self.finishButton.hidden = YES;
            [_activityIndicatorView startAnimating];
        }
            break;
        case HLIMRealVoiceStatusTranslacted:
        {   // 翻译完成
            self.voiceDBView.hidden = YES;
            self.selectButton.hidden = YES;
            self.finishButton.hidden = YES;
            [_activityIndicatorView stopAnimating];
            self.sendButton.hidden = NO;
        }
            break;
            
        default:
            break;
    }
}

- (void)sendClicked:(id)sender
{
    self.status = HLIMRealVoiceStatusRecording;
    if ([self.delegate respondsToSelector:@selector(imkit_realVoiceWillSend)]) {
        [self.delegate imkit_realVoiceWillSend];
    }
}

- (void)finish
{
    if (@protocol(HLIMKItRealVoiceContainerDelegate) && [self.delegate respondsToSelector:@selector(imkit_FinishedSpeakWithHandler:)]) {
        
        if (!_activityIndicatorView) {
            _activityIndicatorView = [UIActivityIndicatorView.alloc initWithFrame:CGRectMake(0, 0, 20, 20)];
            _activityIndicatorView.center = self.voiceDBView.center;
            [self addSubview:_activityIndicatorView];
            
            _activityLabel = [UILabel.alloc init];
            _activityLabel.text = @"识别中...";
            [_activityLabel sizeToFit];
            [self addSubview:_activityLabel];
            
            
            _activityIndicatorView.left = (self.width - CGRectGetWidth(_activityIndicatorView.frame) - 10 - CGRectGetWidth(_activityLabel.frame)) * 0.5;
            _activityLabel.left = _activityIndicatorView.right + 10;
            _activityLabel.centerY = _activityIndicatorView.centerY;
        }
        
        self.status = HLIMRealVoiceStatusTranslacting;
        
        [self.delegate imkit_FinishedSpeakWithHandler:^(BOOL re) {
            
            if (re) {
                self.sendButton.hidden = NO;
                [self->_activityIndicatorView stopAnimating];
                [self->_activityIndicatorView removeFromSuperview];
                self->_activityIndicatorView = nil;
            }
            
        }];
    }
}

#pragma mark - <UITableView>

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(UITableViewCell.class)];
    if (!cell) {
        cell = [UITableViewCell.alloc initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass(UITableViewCell.class)];
        UILabel *label = [UILabel.alloc initWithFrame:CGRectMake(12, 0, 100, 44)];
        label.textColor = [UIColor colorWithRed:168/255.0 green:171/255.0 blue:185/255.0 alpha:1.0];
        label.font = [UIFont systemFontOfSize:15];
        label.textAlignment = NSTextAlignmentLeft;
        label.tag = 199;
        [cell.contentView addSubview:label];
    }
    
    UILabel *label = (UILabel *)[cell.contentView viewWithTag:199];
    label.text = self.languages[indexPath.row];
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.languages.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    [tableView removeFromSuperview];
    _tableView = nil;
    
    NSString *str = self.languages[indexPath.row];
    [self.selectButton setTitle:str forState:UIControlStateNormal];
    
    if ([self.delegate respondsToSelector:@selector(imkit_didSelectedLanguage:)]) {
        [self.delegate imkit_didSelectedLanguage:indexPath.row];
    }
}
@end
